﻿
namespace SRTSolutions.Elevate.Guide
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NUnit.Framework;
    using SRTSolutions.Elevate;
    [TestFixture]
    public class StringThings
    {
        [Test]
        public void ContainsDigit()
        {
            var cond = "nothing here".ContainsDigit();
            Assert.IsFalse(cond);
            cond = "1 little".ContainsDigit();
            Assert.IsTrue(cond);
        }

        [Test]
        public void ContainsSubstring()
        {
            var thisIsNice = "Lillian".Contains("ian");
            var butCaseIgnoringIsNiceToo = 
            "Lillian".Contains("Ian", StringComparison.CurrentCultureIgnoreCase);
            Assert.IsTrue(butCaseIgnoringIsNiceToo);
            Assert.IsFalse("Lill".Contains("Ian", StringComparison.CurrentCultureIgnoreCase));
        }

        [Test]
        public void IncrementTrailingNumber()
        {
            var actual = "Item 1".IncrementTrailingNumber();
            var expected = "Item 2";
            Assert.AreEqual(expected, actual);

            actual = "Group 1 Item 9".IncrementTrailingNumber();
            expected = "Group 1 Item 10";
            Assert.AreEqual(expected, actual);

            actual = "big 2147483646".IncrementTrailingNumber();
            expected = "big 2147483647";
            Assert.AreEqual(expected, actual);

            // caution, it overflows.
            actual = "wow big 9223372036854775807".IncrementTrailingNumber();
            expected = "wow big -9223372036854775808";
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void IncrementTrailingNumberBut9223372036854775807IsAsBigAsItGoes()
        {
            try { "wow big 9223372036854775808".IncrementTrailingNumber(); Assert.Fail(); }
            catch(OverflowException){}
        }

        [Test]
        public void Join()
        {
             var actual = new[] {"it","is","not","a","relational","join"}
                 .Join("-");
             var expected = "it-is-not-a-relational-join";
             Assert.AreEqual(expected, actual);
        }

        [Test]
        public void LevenshteinDistance()
        {
            var kitten = "kitten";
            var sitting = "sitting";
            var d = kitten.GetLevenshteinDistance(sitting);
            Assert.AreEqual(3, d);
            d = kitten.GetLevenshteinDistance("kittens");
            Assert.AreEqual(1, d);
            Assert.AreEqual(3, "Saturday".GetLevenshteinDistance("Sunday"));
        }
        [Test]
        public void Regex()
        {
            //JRW note regex is immutable, but 
            // does not implement equality 
            //var actual = "thisisaregex".Regex();
            //var expected = new System.Text.RegularExpressions.Regex("thisisaregex");
            //Assert.AreEqual(expected., actual);
            var re = "test".Regex();
            var match = re.Match("test");
            Assert.IsTrue(match.Success);
        }

        [Test]
        public void RegexReplace()
        {
            // string.Replace is often not good enough
            // you could .Regex(), but that inverts reading
            var d = "hi mom".RegexReplace("(?:mom|mommy)", "dad");
            Assert.AreEqual("hi dad", d);
        }
    }
}